/*
 * print.c - Print realted
 * Clunix, cezanne@clunix.com, 2004.10
 *
 * Copyright (c) 2004 by Clunix Inc. All Rights Reserved.
 */

#include "config.h"

#include "ecm.h"

int
ecm_asprintf(char **pstr, const char *fmt, ...)
{
	va_list		ap;
	int			ret;

	va_start(ap, fmt);
	ret = ecm_vasprintf(pstr, fmt, ap);
	va_end(ap);
	return ret;
}

char *
ecm_str(const char *fmt, ...)
{
	char		*str;
	va_list		ap;

	va_start(ap, fmt);
	ecm_vasprintf(&str, fmt, ap);
	va_end(ap);
	return str;
}

#ifdef ECM_DEBUG

#define ASPRINTF_ALLOCCHUNK		128

int
ecm_asprintf_internal(char **pstr, const char *fmt, ...)
{
	char	*buf;
	int	buflen = ASPRINTF_ALLOCCHUNK;
	int	retlen;
	va_list	ap;

	va_start(ap, fmt);
again:
	buf = (char *)malloc(buflen);

	retlen = ecm_vsnprintf(buf, buflen, fmt, ap);
	if (retlen >= buflen || retlen < 0) {
		buflen += ASPRINTF_ALLOCCHUNK;
		free(buf);
		goto again;
	}
	*pstr = buf;
	va_end(ap);
	return retlen;
}

#endif
